package com.example.q_kang.hhh.home;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.bumptech.glide.Glide;
import com.example.q_kang.hhh.R;
import com.sskj.base.MVPFragment;
import com.sskj.base.base.App;
import com.sskj.base.bean.QrCodeBean;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.CopyUtil;
import com.sskj.base.utils.ImageUtil;
import com.sskj.base.utils.QRCodeUtil;

import java.lang.reflect.MalformedParameterizedTypeException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author Hey
 * Create at  2019/04/09
 */
public class InviteFriendFragment extends MVPFragment<InviteFriendPresenter> {


    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_url)
    TextView tvUrl;
    @BindView(R.id.btn_copy)
    Button btnCopy;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getQrCode();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_invite_friend;
    }

    @Override
    public InviteFriendPresenter getPresenter() {
        return new InviteFriendPresenter();
    }

    @Override
    public void initView() {
        mPresenter.getQrCode();
        ClickUtil.click(300, btnCopy, view -> {
            CopyUtil.copy(App.INSTANCE, tvUrl.getText().toString());
        });

    }


    @Override
    public void initData() {

    }


    public static InviteFriendFragment newInstance() {
        InviteFriendFragment fragment = new InviteFriendFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    public void getQrCodeSuccess(QrCodeBean qrCodeBean) {
        tvUrl.setText(qrCodeBean.getLink());

        QRCodeUtil.createImage(qrCodeBean.getLink(), (int) RudenessScreenHelper.pt2px(App.INSTANCE, 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                ivQrcode.setImageBitmap(bitmap);
            }

            @Override
            public void onAnalyzeFailed() {

            }
        });
    }
}
